import {
  sub,
  getPatient
} from "../../services/diagnosis"
import {
  nowDate
} from "../../utils/util"
import {
  protocolText,jobs,birthTimes
} from "../../model/userInfoFill"

Page({
  data: {
    endDate: nowDate,
    pickerOptions: [],
    usernameFocus: false,//姓名聚焦
    isReadProtocol: false,//是否读协议
  },
  onLoad() {
    let user_subData = wx.getStorageSync('user_subData')||{
      username: "",//姓名
      sex: 0,//性别
      is_married: 0,//是否结婚
      birth_solar: "",//生日
      birth_time: "",//生辰    
      job: "",//职业     
      isAgree: false,//是否赞同
    }
    let handImageList = wx.getStorageSync('handImageList')
    user_subData =  {
      ...user_subData,
      left_sx: handImageList[0], //左手手心
      left_sb: handImageList[1], //左手手背
      left_dmz: handImageList[2], //左手大拇指
      right_sx: handImageList[3], //右手手心
      right_sb: handImageList[4], //右手手背
      right_dmz: handImageList[5] //右手大拇指
    }
    this.setData({ 
      user_subData,
      jobs: jobs,
      birthTimes: birthTimes,
      protocolText: protocolText,
      jobIndex: jobs.indexOf(user_subData.job||0),
      birth_timeIndex: birthTimes.indexOf(user_subData.birth_time||0)
    })  
    wx.setStorageSync('operate_step', 2)
  },
  // 显示选择就诊人列表
  onStockUserBtnTap() {
    getPatient((data) => {
      this.setData({
        pickerOptions: data.list
      })
      this.selectComponent("#custom-picker").show()
    })
  },
  // 选择诊断人
  onPickerChange(e) {
    
    const {username,sex,is_married,birth_solar,birth_time,job} =  e.detail.value
    let user_subData = {
      ...this.data.user_subData,
      username,//姓名
      sex,//性别
      is_married,//是否结婚
      birth_solar,//生日
      birth_time,//生辰    
      job,//职业     
    }
    this.setData({
      user_subData,
      jobIndex: jobs.indexOf(job||0),
      birth_timeIndex: birthTimes.indexOf(birth_time||0)
    })
    wx.setStorageSync('user_subData', user_subData)
  },
  // 选择工作
  bindJobChange(e) {
    let user_subData = {
      ...this.data.user_subData,
      job: this.data.jobs[e.detail.value]
    }
    this.setData({
      user_subData,
      jobIndex: e.detail.value
    })
    wx.setStorageSync('user_subData', user_subData)
  },
  bindBirthTimeChange(e) {
    let user_subData = {
      ...this.data.user_subData,
      birth_time: this.data.birthTimes[e.detail.value]
    }
    this.setData({
      user_subData,
      birth_timeIndex: e.detail.value
    })
    wx.setStorageSync('user_subData', user_subData)
  },
  bindDateChange: function (e) {
    let user_subData = {
      ...this.data.user_subData,
      birth_solar: e.detail.value
    }
    this.setData({
      user_subData
    })
    wx.setStorageSync('user_subData', user_subData)
  },
  radioChangeSex: function (e) {
    let user_subData =  {
      ...this.data.user_subData,
      sex: e.detail.value
    }
    this.setData({
      user_subData
    })
    wx.setStorageSync('user_subData', user_subData)
  },
  radioChangeMarry: function (e) {
    let user_subData =  {
      ...this.data.user_subData,
      is_married: e.detail.value
    }
    this.setData({
      user_subData
    })
    wx.setStorageSync('user_subData', user_subData)
  },
  handleNameInput(e) {
    let user_subData =  {
      ...this.data.user_subData,
      username: e.detail.value
    }
    this.setData({
      user_subData
    })
    wx.setStorageSync('user_subData', user_subData)
  },
  onUploadTap() {
    const {
      username,
      birth_solar,
      birth_time,
      job,
      isAgree
    } = this.data.user_subData

    if (!username) {
      this.setData({
        usernameFocus: true
      })
      wx.showToast({
        title: '请输入姓名',
        icon: 'none'
      })
      return
    }

    if (!birth_solar) {
      wx.showToast({
        title: '请选择生日',
        icon: 'none'
      })
      return
    }

    if (!birth_time) {
      wx.showToast({
        title: '请选择生辰',
        icon: 'none'
      })
      return
    }

    if (!job) {
      wx.showToast({
        title: '请选择职业',
        icon: 'none'
      })
      return
    }

    if (!isAgree) {
      wx.showToast({
        title: '请阅读并接受隐私协议',
        icon: 'none'
      })
      return
    }
    let data = {
      ... this.data.user_subData,
      loginkey: wx.getStorageSync('loginkey'), //密钥
      mobile: wx.getStorageSync('mobile') //手机号
    }
    delete data.isAgree
    console.log(data)
    sub(async (data) => {
      wx.setStorageSync('users_id', data.infer_user_id)
      wx.removeStorageSync('user_subData')
      wx.removeStorageSync('handImageList')
      wx.removeStorageSync('handImageIndex')
      await wx.p.reLaunch({
        url: '/pages/diag-pay/diag-pay'
      })
    }, data)
  },
  onProtocolBtnTap() {
    this.setData({
      isReadProtocol: true
    })
  },
  onAgreeTap() {
    let user_subData = {
      ...this.data.user_subData,
      isAgree: true
    }
    this.setData({
      user_subData,
      isReadProtocol: false
    })
    wx.setStorageSync('user_subData', user_subData)
  },
  onNoAgreeTap() {
    let user_subData = {
      ...this.data.user_subData,
      isAgree: false
    }
    this.setData({
      user_subData,
      isReadProtocol: false
    })
    wx.setStorageSync('user_subData', user_subData)
  }
})